package my.diplom.dao.impl;

import my.diplom.dao.IHostDAO;
import my.diplom.entity.Host;
import my.diplom.entity.Resource;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.HibernateTemplate;

/**
 * Created by IntelliJ IDEA.
 * User: gb
 * Date: 03.11.2009
 * Time: 1:32:19
 * To change this template use File | Settings | File Templates.
 */
public class HostDAOImpl implements IHostDAO {

     private static final Logger log = Logger.getLogger(HostDAOImpl.class);

    private HibernateTemplate template;

    public HibernateTemplate getTemplate() {
        return template;
    }

    public void setTemplate(HibernateTemplate template) {
        this.template = template;
    }

    @Override
    public void create(Host host) {
        template.save(host);
    }

    @Override
    public void update(Host host) {
        template.saveOrUpdate(host);
    }

    @Override
    public Host getById(Long hostId) {
        return (Host) template.get(Host.class, hostId);
    }

    @Override
    public Host getById(Long hostId, boolean initAll) {
        Host host = (Host) template.get(Host.class, hostId);
        if (initAll){
            for (Resource r: host.getResources()){
                template.initialize(r.getProjects());
                template.initialize(r.getEvents());
            }
        }
        return host;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<Host> getAll() {
         return template.find("from Host order by hostName");
    }

    @Override
    public void delete(Host host) {
        template.delete(host);
    }

    @Override
    public List<Host> getActiveHosts() {
        return template.find("from Host h where h.status.statusName = 'active' order by hostName");
    }
}
